// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: 'cloud-demo-dev-4gvtj96e9f7324c6'
});
const db = cloud.database();
const collection = db.collection('joke');
// 云函数入口函数
exports.main = async (event, context) => {

  const _ = db.command;

  let res = await db.collection('joke').where({
    id: event.id
  }).update({
    data: {
      ['votes.up']:_.inc(event.votes.up),
      ['votes.down']:_.inc(event.votes.down)
    }
  });
  if(res.stats.updated == 0)
  {
    var votes = {up:event.votes.up}
    let res2 = await db.collection('joke').add({
      data: {
        id: event.id,
        votes:event.votes
      }
    })
    return res2;
  }
  else{
    return res;
  }
}